package day04;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @author wangzhisen
 * @Desc TODO
 * @date 2025/4/12
 */

//需求2：有一个数列：8,4,2,1,23,344,12，此时从键盘中任意输入一个数据，判断数列中是否包含此数
//解法：先冒泡排序，再折半查找是否包含

public class TestOneArray02 {
    public static void main(String[] args) {
        int[] arr = {8, 4, 2, 1, 23, 344, 12};
        Arrays.sort(arr);
        System.out.println("排序后："+Arrays.toString(arr));

        Scanner sc=new Scanner(System.in);
        System.out.println("请输入：");
        int a = sc.nextInt();

        //初始值
        int left = 0;
        int right = arr.length - 1;
        int mid = 0;

        //标志位
        boolean flag = false;

        //循环查找
        while (left <= right) {
            mid = (left + right) / 2;

            if(a>arr[mid]){
                //看右边
                left = mid + 1;
            }else if(a<arr[mid]){
                //看左边
                right = mid - 1;
            }else if(a==arr[mid]){
                flag = true;
                System.out.println("存在");
                break;
            }
        }
        if(!flag){
            System.out.println("不包含");
        }
    }
}
